<?php
class food_item extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('food_category_model', 'food_category');
		$this->load->model('food_model', 'food');
        $this->load->library('session');
    }

    function index()
    {
        $message = $this->session->flashdata('message');
        $category = $this->food_category->get_category();
        // no food categories yet.
        if(count($category) == 0)
        {
            $msg = "please add some categories before add items";
            $this->session->set_flashdata('message', $msg);
            $this->load->helper('url');
            redirect("/admin/food_category/");
        }
        $cat_select = (int)$this->input->post('cat_select');
        
        if($cat_select == 0)
        {
            $first_category = current($category);
            $cat_select = $first_category['id'];
        }
        
        $foods = $this->food->get_food($cat_select);

        $data = array(
            'food_category' => $category,
            'cat_select' => $cat_select,
            'foods' => $foods,
            'page_title' => 'food item',
            'page_css' => array('admin.css'),
            'page_js' => array(),
            'message' => $message
        );

        $this->load->view('admin/food_item', $data);
    }
    function read()
    {
        $id = $this->input->post('target_id');
        if($this->input->is_ajax_request()){
		    $food = $this->food->read($id, FALSE);
		    echo json_encode($food, JSON_HEX_TAG | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_HEX_APOS);
        }
    }
    function add()
    {
        $is_spicy = 0;
        $is_spicy_cb = $this->input->post('is_spicy');
        if($is_spicy_cb === 'on'){
            $is_spicy = 1;
        }
        $sequence = $this->input->post('row_select') . "-" . $this->input->post('col_select');
        $result = $this->food->create($this->input->post('category_id'), $this->input->post('en_name'),
            $this->input->post('ch_name'), $sequence, $is_spicy,
            $this->input->post('size1en_name'), $this->input->post('size1ch_name'),$this->input->post('size1price'),
            $this->input->post('size2en_name'), $this->input->post('size2ch_name'),$this->input->post('size2price'),
            $this->input->post('size3en_name'), $this->input->post('size3ch_name'),$this->input->post('size3price')
        );
        if(count($result['user']) === 0 || array_key_exists('note', $result) === true){
            $this->session->set_flashdata('message', 'add successful');

        }
        else{
            $msg = "";
            foreach($result['user'] as $error){
                $msg .=  $error . "<br />";
            }
            $this->session->set_flashdata('message', $msg);
        }
        $this->load->helper('url');
        redirect("/admin/food_item/");
    }


    function update()
    {
        $is_spicy = 0;
        $is_spicy_cb = $this->input->post('is_spicy');
        if($is_spicy_cb === 'on'){
            $is_spicy = 1;
        }
        $sequence = $this->input->post('row_select') . "-" . $this->input->post('col_select');
        $result = $this->food->update($this->input->post('target_id'), $this->input->post('category_id'),
            $this->input->post('en_name'), $this->input->post('ch_name'), $sequence, $is_spicy,
            $this->input->post('size1en_name'), $this->input->post('size1ch_name'),$this->input->post('size1price'),
            $this->input->post('size2en_name'), $this->input->post('size2ch_name'),$this->input->post('size2price'),
            $this->input->post('size3en_name'), $this->input->post('size3ch_name'),$this->input->post('size3price')
        );
        if(count($result['user']) === 0 || array_key_exists('note', $result) === true){
            $this->session->set_flashdata('message', 'update successful');

        }
        else{
            $msg = "";
            foreach($result['user'] as $error){
                $msg .=  $error . "<br />";
            }
            $this->session->set_flashdata('message', $msg);
        }
        $this->load->helper('url');
        redirect("/admin/food_item/");
    }
    /**
	 * delete food category
	 */
	function delete()
	{
		$sequence = $this->input->post('delete_sequence');
        $del_a = explode("_", $sequence, 2);
        $result = $this->food->delete($del_a[0]);
        if(count($result['user']) === 0){
            $this->session->set_flashdata('message', 'delete successful');
        }
        else{
            $msg = "";
            foreach($result['user'] as $error){
                $msg .=  $error . "<br />";
            }
            $this->session->set_flashdata('message', $msg);
        }
        $this->load->helper('url');
        redirect("/admin/food_item/");
	}

    /**
     * swap food category
     */
    function swap()
    {
        $from_sequence = $this->input->post('swap_from');
        $to_sequence = $this->input->post('swap_to');
        $from_a = explode("_", $from_sequence, 2);
        $to_a = explode("_", $to_sequence, 2);
        $result = $this->food->swap($from_a[0], $to_a[0]);
        if(count($result['user']) === 0){
            $this->session->set_flashdata('message', 'swap successful');
        }
        else{
            $msg = "";
            foreach($result['user'] as $error){
                $msg .=  $error . "<br />";
            }
            $this->session->set_flashdata('message', $msg);
        }
        $this->load->helper('url');
        redirect("/admin/food_item/");
    }
}